package org.zachary.agent.infrastructure.repository;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.AbstractAggregateRoot;
import org.springframework.stereotype.Repository;
import org.zachary.agent.domain.aggregate_model.loginUser.LoginUser;
import org.zachary.agent.domain.aggregate_model.user.ProfilePicture;
import org.zachary.agent.domain.aggregate_model.user.User;
import org.zachary.agent.domain.aggregate_model.user.Username;
import org.zachary.agent.domain.repository.UserRepository;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

@Repository
public class JpaUserRepository implements UserRepository {
    private final SpringDataUserRepository jpaRepo;

    public JpaUserRepository(SpringDataUserRepository jpaRepo) {
        this.jpaRepo = jpaRepo;
    }

    @Override
    public User findByUsername(String username) {
        Username name=new Username(username);
        User repo = jpaRepo.findByUsername(name);
        System.out.println(repo);
        return repo;
    }

    @Override
    public void save(User user) {
        jpaRepo.save(user);
    }



    @Override
    public User findByUuid(String uuid) {

        return jpaRepo.queryByUuid(uuid);
    }

    @Override
    public void improveUserInfo(String uuid,User user) {

        jpaRepo.updateUserByUuid(uuid,user);
        System.out.println("JPAUserImpl ");
    }

    @Override
    public User findById(Integer id) {
        return jpaRepo.findByIntegerId(id);
    }

    @Override
    public void updateAvatar(Integer userId, String url) {
        jpaRepo.updateUserAvatar(userId,new ProfilePicture(url));
    }



    @Override
    public Map<Integer, UserIdCountryProjection> loadCountyByIds(List<Integer> ids) {
        List<UserIdCountryProjection> list = jpaRepo.loadCountiesByIds(ids);
        System.out.println("jpa:"+list);
        return list.stream()
                .collect(Collectors.toMap(UserIdCountryProjection::getId, Function.identity()));
    }

}

